//
//  RootViewController.m
//  PaypalDemo
//
//  Created by 永来 付 on 15/3/17.
//  Copyright (c) 2015年 fuyonglai. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()
{
    Braintree *_braintree;
    UIButton *payPalButton;
}

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _braintree = [Braintree braintreeWithClientToken:@"eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiJiODNkY2U4ZGIzYmIzZmU4NzAxY2Q0YjdkNjQ3ZDhjNTk5NjFhZjNiNTJjNTYwMDViODJkNzE4NjUzYThlNWU3fGNyZWF0ZWRfYXQ9MjAxNS0wMy0xN1QwOTozODo0NC40MjUyNTU4NTcrMDAwMFx1MDAyNm1lcmNoYW50X2lkPXQ1OTNnZm56ZzU4ZDQ1bmRcdTAwMjZwdWJsaWNfa2V5PTdxNHljNXdudDk3ODd0Y3oiLCJjb25maWdVcmwiOiJodHRwczovL2FwaS5icmFpbnRyZWVnYXRld2F5LmNvbTo0NDMvbWVyY2hhbnRzL3Q1OTNnZm56ZzU4ZDQ1bmQvY2xpZW50X2FwaS92MS9jb25maWd1cmF0aW9uIiwiY2hhbGxlbmdlcyI6W10sImNsaWVudEFwaVVybCI6Imh0dHBzOi8vYXBpLmJyYWludHJlZWdhdGV3YXkuY29tOjQ0My9tZXJjaGFudHMvdDU5M2dmbnpnNThkNDVuZC9jbGllbnRfYXBpIiwiYXNzZXRzVXJsIjoiaHR0cHM6Ly9hc3NldHMuYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJhdXRoVXJsIjoiaHR0cHM6Ly9hdXRoLnZlbm1vLmNvbSIsImFuYWx5dGljcyI6eyJ1cmwiOiJodHRwczovL2NsaWVudC1hbmFseXRpY3MuYnJhaW50cmVlZ2F0ZXdheS5jb20ifSwidGhyZWVEU2VjdXJlRW5hYmxlZCI6ZmFsc2UsInBheXBhbEVuYWJsZWQiOmZhbHNlLCJjb2luYmFzZUVuYWJsZWQiOmZhbHNlLCJtZXJjaGFudElkIjoidDU5M2dmbnpnNThkNDVuZCIsInZlbm1vIjoib2ZmIn0="];

    self.view.backgroundColor = [UIColor redColor];
    
    BTPaymentButton *button = [_braintree paymentButtonWithDelegate:self];
    [button setFrame:CGRectMake(0,0,60,120)];
    button.backgroundColor = [UIColor blackColor];
    [self.view addSubview:button];
    
    NSOrderedSet *types = [NSOrderedSet orderedSetWithObjects:@(BTPaymentProviderTypeVenmo), nil];
    BTPaymentButton *button1 = [_braintree paymentButtonWithDelegate:self paymentProviderTypes:types];
    button1.frame = CGRectMake(0, 100, 120, 40);
    button1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:button1];
    
    [self performSelector:@selector(showView) withObject:nil afterDelay:0.5];
    
}

- (void)paymentMethodCreator:(id)sender didCreatePaymentMethod:(BTPaymentMethod *)paymentMethod {
    NSLog(@"123");
}

- (void)showView {
    
    
    
    BTDropInViewController *dropInVC = [_braintree dropInViewControllerWithDelegate:self];
    dropInVC.summaryTitle = @"1 Item";
    dropInVC.summaryDescription = @"15 feet by 12 feet";
    
    dropInVC.displayAmount = @"$10";
    dropInVC.callToActionText = @"Subscribe Now";
//    dropInVC.shouldHideCallToAction = YES;
    
    dropInVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                              target:self
                                                                                              action:@selector(userDidCancelPayment)];
    
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:dropInVC];

    [self presentViewController:navigationController
                       animated:YES
                     completion:nil];
    
//    [self presentViewController:[UIViewController new] animated:YES completion:^{
//        
//    }];
}

- (void)userDidCancelPayment {
    [self dismissViewControllerAnimated:YES completion:nil];
}
    
- (void)dropInViewController:(__unused BTDropInViewController *)viewController didSucceedWithPaymentMethod:(BTPaymentMethod *)paymentMethod {
//    self.nonce = paymentMethod.nonce;
//    [self postNonceToServer:self.nonce]; // Send payment method nonce to your server
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)dropInViewControllerDidCancel:(__unused BTDropInViewController *)viewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
